{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we define the last time interval as [a, b), the current time interval as [c, d)\n",
    "\n",
    "We can use this formula to determine if the current time interval is a subset of last time interval:\n",
    "\n",
    "`(d<=b and d>a) or (c>=a and c<b) or (c<=a and d>=b)`\n",
    "\n",
    "> I got this from a free drawing experience on whiteboard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "inputs = \"\"\"\n",
    "[\"MyCalendar\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\",\"book\"]\n",
    "[[],[97,100],[33,51],[89,100],[83,100],[75,92],[76,95],[19,30],[53,63],[8,23],[18,37],[87,100],[83,100],[54,67],[35,48],[58,75],[70,89],[13,32],[44,63],[51,62],[2,15]]\n",
    "\"\"\".strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputs1 = \"\"\"\n",
    "[null,true,true,false,false,true,false,true,true,false,false,false,false,false,false,false,false,false,false,false,true]\n",
    "\"\"\".strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputs2 = \"\"\"\n",
    "[null,true,true,false,false,true,false,true,true,false,false,false,false,false,false,false,false,true,false,false,false]\n",
    "\"\"\".strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] None None\n",
      "[97, 100] True True\n",
      "---\n",
      "[33, 51] True True\n",
      "---\n",
      "[89, 100] False False\n",
      "[83, 100] False False\n",
      "[75, 92] True True\n",
      "---\n",
      "[76, 95] False False\n",
      "[19, 30] True True\n",
      "---\n",
      "[53, 63] True True\n",
      "---\n",
      "[8, 23] False False\n",
      "[18, 37] False False\n",
      "[87, 100] False False\n",
      "[83, 100] False False\n",
      "[54, 67] False False\n",
      "[35, 48] False False\n",
      "[58, 75] False False\n",
      "[70, 89] False False\n",
      "[13, 32] False True\n",
      "---\n",
      "[44, 63] False False\n",
      "[51, 62] False False\n",
      "[2, 15] True False\n",
      "---\n"
     ]
    }
   ],
   "source": [
    "for a, b, c in zip(json.loads(inputs.split(\"\\n\")[1]), json.loads(outputs1), json.loads(outputs2)):\n",
    "    print(a, b, c)\n",
    "    if b or c:\n",
    "        print(\"---\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/my-calendar-i\n",
    "\n",
    "\n",
    "Runtime: 2520 ms, faster than 5.04% of Python3 online submissions for My Calendar I.\n",
    "Memory Usage: 14.9 MB, less than 22.67% of Python3 online submissions for My Calendar I.\n",
    "\n",
    "\n",
    "\n",
    "```python\n",
    "class MyCalendar:\n",
    "\n",
    "    def __init__(self):\n",
    "        self.lists = []\n",
    "        \n",
    "    def is_ok(self, a,b,c,d):\n",
    "        return not ((d<=b and d>a) or (c>=a and c<b) or (c<=a and d>=b))\n",
    "\n",
    "    def book(self, start: int, end: int) -> bool:\n",
    "        if all(self.is_ok(l[0], l[1], start, end) for l in self.lists):\n",
    "            self.lists.append((start, end))\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
